package com.bjc.core.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjc.base.bean.PermissionTreeVO;
import com.bjc.base.core.BaseServiceImpl;
import com.bjc.base.core.IBaseDao;
import com.bjc.core.bean.request.DepartmentReqVO;
import com.bjc.core.bean.request.RolePermissionReqVO;
import com.bjc.core.bean.request.UserRoleReqVO;
import com.bjc.core.bean.response.DepartmentVO;
import com.bjc.core.bean.response.DimensionItemDataVO;
import com.bjc.core.bean.response.UserRoleVO;
import com.bjc.core.dao.IDepartmentDao;
import com.bjc.core.dao.IUserRoleDao;
import com.bjc.core.service.IDepartmentService;

/**
 * 部门业务实现
 * 
 * @author 许自辉
 * @date 2017年8月21日
 */
@Service
public class DepartmentImpl extends BaseServiceImpl<DepartmentReqVO, DepartmentVO> implements IDepartmentService {


	private IDepartmentDao departmentDao;
	@Autowired
	private IUserRoleDao userRoleDao;
	
	@Autowired
	@Override
	public void setBaseDao(IBaseDao<DepartmentReqVO, DepartmentVO> baseDao) {
		this.baseDao = baseDao;
		this.departmentDao = (IDepartmentDao)baseDao;
	}

	@Override
	public List<DimensionItemDataVO> getDimensionItemData(DepartmentReqVO dataReqVO) {
		return departmentDao.getDimensionItemData(dataReqVO);
	}

	@Override
	public List<PermissionTreeVO> getTree(RolePermissionReqVO rolepermissionReqVO) {
		return departmentDao.getTree(rolepermissionReqVO);
	}

	@Override
	public List<Integer> getChildDept(String workId) {
		//1,部门
		//获取当前登录人的角色
		UserRoleReqVO role = new UserRoleReqVO();
		role.setWorkId(workId);
		List<UserRoleVO> roleList= userRoleDao.getList(role);
		if(roleList !=null && roleList.size()>0){
			StringBuffer roles =new StringBuffer();
			String roleStr ="";
			for (UserRoleVO userRoleVO : roleList) {
				roles.append(userRoleVO.getRoleId());
				roles.append(",");
			}
			roleStr = roles.toString().substring(0, roles.length()-1);
			List<Integer> str = departmentDao.getDimensionData(workId,roleStr);
			DepartmentReqVO dataReqVO = new DepartmentReqVO();
			dataReqVO.setDeptIds(str);
			if(str!=null && str.size()>0){
				List<DepartmentVO> deptList = departmentDao.getChildDept(dataReqVO);
				List<Integer> deptIds = new ArrayList<Integer>();
				for (DepartmentVO departmentVO : deptList) {
					deptIds.add(departmentVO.getId());
				}
				return deptIds;
			}
			return null;
		}
		return null;
		
	}

	

}
